package net.aiotos.common.dto;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import net.aiotos.common.exception.*;

import java.util.List;

@lombok.NoArgsConstructor
@lombok.Data
public class LoginResp {

    /**
     * user_id : 457
     * token : ubhk64ld41ogz2iqpfv0wrv951w3p662
     * code : 0
     * product_list : []
     * retry : {"interval":3000,"times":10}
     * time : 1.618798736464881E9
     * msg : OK
     * heartbeat : 50
     * data : [{"lastAt":0,"tableId":"ff48443691f011ebaad4000c2988ff06?session=ubhk64ld41ogz2iqpfv0wrv951w3p662","name":"mqtt","onTime":1617692053,"online":0,"owner":"self","timestamp":1617692053,"desc":""}]
     * router_config : {"routerPrxPort":4064,"remoteAdapterPort":10077,"iotrouterIP":"43.226.65.51"}
     */

    @JSONField(name = "user_id")
    private Integer userId;
    /**
     * 平台APITOKEN
     */
    @JSONField(name = "token")
    private String token;
    @JSONField(name = "code")
    private Integer code;
    @JSONField(name = "retry")
    private RetryDTO retry;
    @JSONField(name = "time")
    private Double time;
    @JSONField(name = "msg")
    private String msg;
    @JSONField(name = "heartbeat")
    private Integer heartbeat;
    /**
     * 通信路由服务器连接参数
     */
    @JSONField(name = "router_config")
    private RouterConfigDTO routerConfig;
    @JSONField(name = "product_list")
    private List<?> productList;
    @JSONField(name = "data")
    private List<DataDTO> data;

    @lombok.NoArgsConstructor
    @lombok.Data
    public static class RetryDTO {
        /**
         * interval : 3000
         * times : 10
         */

        @JSONField(name = "interval")
        private Integer interval;
        @JSONField(name = "times")
        private Integer times;
    }

    @lombok.NoArgsConstructor
    @lombok.Data
    public static class RouterConfigDTO {
        /**
         * routerPrxPort : 4064
         * remoteAdapterPort : 10077
         * iotrouterIP : 43.226.65.51
         */

        @JSONField(name = "routerPrxPort")
        private Integer routerPrxPort;
        @JSONField(name = "remoteAdapterPort")
        private Integer remoteAdapterPort;
        @JSONField(name = "iotrouterIP")
        private String iotrouterIP;
    }

    @lombok.NoArgsConstructor
    @lombok.Data
    public static class DataDTO {
        /**
         * lastAt : 0
         * tableId : ff48443691f011ebaad4000c2988ff06?session=ubhk64ld41ogz2iqpfv0wrv951w3p662
         * name : mqtt
         * onTime : 1617692053
         * online : 0
         * owner : self
         * timestamp : 1617692053
         * desc :
         */

        @JSONField(name = "lastAt")
        private String lastAt;
        @JSONField(name = "tableId")
        private String tableId;
        @JSONField(name = "name")
        private String name;
        @JSONField(name = "onTime")
        private Integer onTime;
        @JSONField(name = "online")
        private Integer online;
        @JSONField(name = "owner")
        private String owner;
        @JSONField(name = "timestamp")
        private Integer timestamp;
        @JSONField(name = "desc")
        private String desc;
    }


    /**
     * 异常检测，统一处理非0返回void
     */
    public void check() throws Exception {
        if(code == ErrorEnum.Successfull.code) {
            return;
        }

        if(code == ErrorEnum.SelfOffline.code) {
            throw new SelfOfflineError();
        }
        if(code == ErrorEnum.AccountNotRegister.code) {
            throw new AccountNotRegisterError();
        }
        if(code == ErrorEnum.IONodeNotExist.code) {
            throw new IONodeNotExistError();
        }
        if(code == ErrorEnum.DataNotExist.code) {
            throw new DataNotExistError("");
        }
        if(code == ErrorEnum.RequestNotAuth.code) {
            throw new RequestNotAuthError("");
        }
//        if(code == ErrorEnum.Unknown) {
//            throw new Exception("未定义:" + rawJson);
//        }
        throw new Exception(JSONObject.toJSONString(this));
    }

    /**
     * 获取UUID SESSION
     * @return fa7a4e52e48611ebad2afa163e396af4?session=xlb8f1xns8ghn4ni2mbd99golreyyxnz
     */
    public String getUuidSession() {
        return data.get(0).getTableId();
    }
}
